<?php

namespace sms_cs\driver;

use Yunpian\Sdk\Constant\YunpianConstant as YC;
use Yunpian\Sdk\Util\ApiUtil;

/**
 * 云片网短信接口
 * @version 2018-4-13
 */
class YunpianSMS {

    protected $clnt;

    public function __construct($apikey) {
        $this->clnt = \Yunpian\Sdk\YunpianClient::create($apikey);
    }

    /**
     * 发送单条短信
     * @param type $mobile
     * @param type $text
     * @return array array('code'=>0,'msg'=>'发送成功','detail'=>NULL,'data'=>array('code'=>0,'msg'=>'发送成功','count'=>1,'fee'=>0.048000000000000001,'unit'=>'RMB','mobile'=>'13950076987','sid'=>23276592186,),)
     */
    function single_send($mobile, $text = '') {
        $param = [YC::MOBILE => $mobile, YC::TEXT => '【一代佳人】您的验证码是1234'];
        $result = $this->clnt->sms()->single_send($param);
        return ['code' => $result->code(), 'msg' => $result->msg(), 'detail' => $result->detail(), 'data' => $result->data()];
    }

    /**
     * 批量发送短信
     * @param string $mobile 发送多个手机号请以逗号分隔，一次不要超过1000个
     * @param type $text
     * @return type
     */
    function batch_send($mobiles, $text) {
        $param = [YC::MOBILE => $mobiles, YC::TEXT => '【云片网】您的验证码是1234'];
        return $this->clnt->sms()->batch_send($param);
    }

    /**
     * 批量发送短信
     * @param type $mobiles
     * @param type $texts
     * @return type
     */
    function multi_send($mobiles, $texts) {
        $param = [YC::MOBILE => '18616020610,18616020611',
            YC::TEXT => ApiUtil::urlEncodeAndLink(['【哈哈哈】您的验证码,是1234', '【哈哈哈】您的验证码是1234'])];
        return $this->clnt->sms()->multi_send($param);
    }

    /**
     *
     * @deprecated
     *
     */
    function tpl_single_send() {
        $param = [YC::MOBILE => '18616020000', YC::TPL_ID => '1', YC::TPL_VALUE => '#company#=云片网'];
        return $this->clnt->sms()->tpl_single_send($param);
    }

    /**
     *
     * @deprecated
     *
     */
    function tpl_batch_send() {
        $param = [YC::MOBILE => '18616020000', YC::TPL_ID => '1', YC::TPL_VALUE => '#company#=云片网'];
        return $this->clnt->sms()->tpl_batch_send($param);
    }

    function pull_status() {
        $param = [YC::PAGE_SIZE => '20'];
        return $this->clnt->sms()->pull_status($param);
        // v1
        return $this->clnt->sms()->version(YC::VERSION_V1)->pull_status($param);
    }

    function pull_reply() {
        $param = [YC::PAGE_SIZE => '20'];
        return $this->clnt->sms()->pull_reply($param);

        // v1
        // return $this->clnt->sms()->version(YC::VERSION_V1)->pull_reply($param);
    }

    function get_record() {
        $param = [YC::START_TIME => '2013-08-11 00:00:00', YC::END_TIME => '2016-12-05 00:00:00', YC::PAGE_NUM => '1',
            YC::PAGE_SIZE => '20'];
        return $this->clnt->sms()->get_record($param);
        // v1
        // return $this->clnt->sms()->version(YC::VERSION_V1)->get_record($param);
    }

    function get_black_word() {
        $param = [YC::TEXT => '高利贷,发票'];
        return $this->clnt->sms()->get_black_word($param);
        // v1
        // return $this->clnt->sms()->version(YC::VERSION_V1)->get_black_word($param);
    }

    /**
     *
     * @deprecated
     *
     */
    function send() {
        $param = [YC::MOBILE => '18616020000', YC::TEXT => '【云片网】您的验证码是1234'];
        return $this->clnt->sms()->version(YC::VERSION_V1)->send($param);
    }

    function get_reply() {
        $param = [YC::START_TIME => '2013-08-11 00:00:00', YC::END_TIME => '2016-12-05 00:00:00', YC::PAGE_NUM => '1',
            YC::PAGE_SIZE => '20'];
        return $this->clnt->sms()->get_reply($param);
        // v1
        // return $this->clnt->sms()->version(YC::VERSION_V1)->get_reply($param);
    }

    /**
     *
     * @deprecated
     *
     */
    function tpl_send() {
        $param = [YC::MOBILE => '18616020000', YC::TPL_ID => '1', YC::TPL_VALUE => '#company#=云片网'];
        return $this->clnt->sms()->version(YC::VERSION_V1)->tpl_send($param);
    }

    function count() {
        $param = [YC::START_TIME => '2013-08-11 00:00:00', YC::END_TIME => '2016-12-05 00:00:00', YC::PAGE_NUM => '1',
            YC::PAGE_SIZE => '20'];
        // v1
        return $this->clnt->sms()->version(YC::VERSION_V1)->count($param);
        // don't invoke count with v2,result is invalid json
        // return $this->clnt->sms()->count($param);
    }

}
